#ifndef YHAIDA_ACCEPTOR_H
#define YHAIDA_ACCEPTOR_H
#include "../base/Utils.h"
#include "Socket.h"
#include "../logging/Logging.h"
#include "SocketsOps.h"
#include "Channel.h"
#include <functional>
#include <fcntl.h>

namespace yhaida
{
    //前向声明
    class EventLoop;
    class InetAddress;

    class Acceptor : Utils::nocapyable
    {
    public:
        typedef std::function<void(int socket, const InetAddress& )> NewConnectionCallback;

    public:
        Acceptor(EventLoop* loop,const InetAddress& listenAddr,bool reuseport);
        ~Acceptor()=default;

        void listen();
        bool listenning()const { return _listenning; }

        void setNewConnectionCallback(const NewConnectionCallback &cb)
        {
            _newConnectionCallback = cb;
        }

    private:
        void handleRead();

        EventLoop *_mainEventLoop;

        Socket _acceptorFd;
        Channel _acceptChannel;
        NewConnectionCallback _newConnectionCallback;

        bool _listenning;

        int _idleFd;//空白的占位文件描述符
    };
}

#endif